/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class LoginstafServlet extends HttpServlet {

    final String VALID_NAME = "staff";
    final String PASSWORD = "1234567890";
    final String CODE_VALID = "3g09I8Lm7Dw4N";

    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)
                         throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        //form HTML untuk login
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Halaman Staff</title>");
        out.println("</head>");
        out.println("<body>");

        out.println("<center><h1>Halaman Staff</h1></center>");
        
        out.println("<p>Silahkan Login : </p>");

        out.println("<form name='login' action='LoginstafServlet' " +
                    "method='POST'>");
        out.println("<p>User Name : ");
        out.println("<input type='text' name='userName' value='' " +
                    "width='24' />");
        out.println("</p><p>Password: ");
        out.println("<input type='password' name='userPwd' value='' " +
                    "width='24' />");
        out.println("</p><br>");
        out.println("<input type='reset' value='Reset' " +
                    "name='reset' />  ");
        out.println("<input type='submit' value='Login' " +
                    "name='submit' />");
        out.println("</form>");

        out.println("</body>");
        out.println("</html>");

        out.close();
    }

    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response)
                          throws ServletException, IOException {
        //ambil parameter nama dan password
        String userName = request.getParameter("userName");
        String password = request.getParameter("userPwd");
        boolean valid = false;

        //cek identitas login
        if ((userName.equalsIgnoreCase(VALID_NAME)) &&
            (password.equals(PASSWORD))) {
            valid = true;
        }

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        if (!valid) {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Error Page</title>");
            out.println("</head>");
            out.println("<body>");

            out.println("<h1>Invalid logins!</h1><br>");
            out.println("<h2><a href=\"LoginstafServlet\">Back...</a></h2>");

            out.println("</body>");
            out.println("</html>");

            out.close();
            return;
        }

        //buat cookie
        Cookie ckName = new Cookie("Pref", userName);
        Cookie ckPwd = new Cookie("Vid", CODE_VALID);
        ckName.setMaxAge(60*60);   // 1 jam
        ckPwd.setMaxAge(60*60);   // 1 jam
        response.addCookie(ckName);
        response.addCookie(ckPwd);

        //link ke halaman content
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Content Link</title>");
        out.println("</head>");
        out.println("<body>");

        out.println("<h1>Login success!</h1><br>");
        out.println("<h3>selamat datang dihalaman staff, klik enter untuk lanjutkan" +
                    "...</h3>");
        out.println("<form name='secret' action='actionstaff.jsp' " +
                    "method='POST'>");
        out.println("<p><input type='submit' value='Enter' " +
                    "name='submit' /></p>");
     
      
        out.println("</form><hr>");
        out.println("</body>");
        out.println("</html>");

        out.close();
    }

    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Login Servlet";
    }
}
